package edu.must.uh.servlet;

import java.util.Map;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import edu.must.uh.bean.DeployQueryResultBean;
import edu.must.uh.impl.AttendancdAdjustmentImpl;
import edu.must.uh.util.R;
import edu.must.uh.util.ServletUtils;
import edu.must.uh.util.Tools;
import edu.must.uh.util.ToolsOfString;

@WebServlet("/AttendancdAdjustmentServlet")
public class AttendancdAdjustmentServlet extends BaseServlet {
	
    private static final long serialVersionUID = 12197442526341123L;
    private static Logger logger = Logger.getLogger(AttendancdAdjustmentServlet.class);  
    AttendancdAdjustmentImpl dataImpl = new AttendancdAdjustmentImpl();  
    
    public Object getList(HttpServletRequest request, HttpServletResponse response) throws Exception {      
     
    	
        dataImpl._PageBean = super.pageBean;
        Map<String, Object> searchcondition = ServletUtils.getsearchcondition(request);
        String sqlString = "";
        if(searchcondition !=null && !searchcondition.isEmpty() && searchcondition.get("ATTEND_DATE") !=null) {
        	String ATTEND_DATE =ToolsOfString.NulltoEmpty(searchcondition.get("ATTEND_DATE")) ;
        	sqlString =" AND DATE_FORMAT(ATTEND_DATE,'%Y-%m') = '"+ATTEND_DATE+"'  ";	
        	searchcondition.put("ATTEND_DATE", "");
        }
        DeployQueryResultBean resList = dataImpl.getLimitlist(searchcondition, sqlString +" ORDER BY ATTEND_DATE desc,CREATETIME desc ");                 
        for (Map<String, Object> map : resList.getResultList()) {
                   
            ServletUtils.createDateHelper(map,"ATTEND_DATE"); 
        }
        
        return R.succ(resList.getResultList(),resList.getTotal());         
    }
    

    
    public Object getOneData(HttpServletRequest request, HttpServletResponse response) throws Exception { 
    	String keyValue =ToolsOfString.NulltoEmpty(request.getParameter("ID")); 
    	String sqlString = "SELECT  ID,UserId,Account,RealName,ATTEND_DATE,ORIGCODE,ORIGPERIOD,ORIGTIMETYPE,CODE,PERIOD,ENABLEMARK,TIMETYPE,TIMEPERIOD,REMARK,ORIGTYPE,TYPE,REMARK FROM v_attendance_adjustment Where DeleteMark= 0  ";
    	sqlString = sqlString + " AND ID = '"+keyValue+"'";
        Map<String,Object>  map = dataImpl.getMap(sqlString); 
        ServletUtils.createDateHelper(map,"ATTEND_DATE"); 
        return R.succ(map);         
    }
    
    public Object delete(HttpServletRequest request, HttpServletResponse response) throws Exception {      
            
        String keyValue =ToolsOfString.NulltoEmpty(request.getParameter("ID")); 
        Integer resInteger = dataImpl.delete(keyValue);
        return R.succ(resInteger);         
    }
    
  public Object save(HttpServletRequest request, HttpServletResponse response) throws Exception {  
      		
		String sql = "";
		Map<String, Object> searchcondition = ServletUtils.getsearchcondition(request);
	    String ID =ToolsOfString.NulltoEmpty(searchcondition.get("ID"));        
        String UserId =ToolsOfString.NulltoEmpty(searchcondition.get("UserId"));
        String ATTEND_DATE =ToolsOfString.NulltoEmpty(searchcondition.get("ATTEND_DATE"));
        
        String ORIGCODE =ToolsOfString.NulltoEmpty(searchcondition.get("ORIGCODE"));
        String ORIGPERIOD =ToolsOfString.NulltoEmpty(searchcondition.get("ORIGPERIOD"));
        String ORIGTYPE =ToolsOfString.NulltoEmpty(searchcondition.get("ORIGTYPE"));    
        String ORIGTIMETYPE =ToolsOfString.NulltoEmpty(searchcondition.get("ORIGTIMETYPE"));
       
        
        String CODE =ToolsOfString.NulltoEmpty(searchcondition.get("CODE"));
        String PERIOD =ToolsOfString.NulltoEmpty(searchcondition.get("PERIOD"));
        String TYPE =ToolsOfString.NulltoEmpty(searchcondition.get("TYPE"));
        String TIMEPERIOD =ToolsOfString.NulltoEmpty(searchcondition.get("TIMEPERIOD"));
        String TIMETYPE =ToolsOfString.NulltoEmpty(searchcondition.get("TIMETYPE"));
        
        String REMARK =ToolsOfString.NulltoEmpty(searchcondition.get("REMARK"));
        REMARK =  java.net.URLDecoder.decode(REMARK, "UTF-8");
       
        if(Tools.notEmpty(ID)) {
        	sql = "UPDATE attendance_adjustment  SET ATTEND_DATE = '"+ATTEND_DATE+"',ORIGCODE = '"+ORIGCODE+"',ORIGTIMETYPE = '" +ORIGTIMETYPE+"' ";
        	sql = sql + " ,ORIGPERIOD = '" +ORIGPERIOD+"',CODE = '" +CODE+"',TYPE = '" +TYPE+"',PERIOD = '" +PERIOD+"',TIMETYPE = '" +TIMETYPE+"',TIMEPERIOD = '" +TIMEPERIOD+"',REMARK = '" +REMARK+"'";
        	sql = sql + " WHERE ID ='"+ID+"'"; 
        }else {
            ID = Tools.getUUID32();
        	sql = "INSERT INTO attendance_adjustment(ID,UserId,ATTEND_DATE,ORIGCODE,ORIGTYPE,ORIGTIMETYPE,ORIGPERIOD,CODE,TIMETYPE,PERIOD,TIMEPERIOD,REMARK,CREATETIME,TYPE)VALUES";
        	sql = sql + " ('"+ID+"','"+UserId+"','"+ATTEND_DATE+"','"+ORIGCODE+"','"+ORIGTYPE+"','"+ORIGTIMETYPE+"','"+ORIGPERIOD+"','"+CODE+"','"+TIMETYPE+"','"+PERIOD+"','"+TIMEPERIOD+"','"+REMARK+"',now(),'"+TYPE+"')"; 
        }
        Integer resInteger = dataImpl.save(sql);
        if(resInteger == 1) {
        	   return R.succ(); 
        }
        return R.fail();
       
    }
    
    
    
   
}
